﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Entities;

namespace CuaHangBangDia.QLKHThue
{
    public partial class ChiTietKHTHueForm : Form
    {
        public ChiTietKHTHueForm()
        {
            InitializeComponent();
        }
        private int item = 20;
        private int tong;
        private int page = 1;
        public static int TongTrang(int soluongdl, int num)
        {
            int kq = (int)(soluongdl / num);
            if (kq * num == soluongdl)
                return kq;
            return kq + 1;
        }
        private void LoadKH(List<ChiTietKhachHangThue> kh)
        {
            dgvChiTietKHThue.DataSource = kh;
            tong = TongTrang(ChiTietKhachHangThue.Objects.GetAll().ToList().Count, item);
            txtTrang.Text = Convert.ToString(page);
            lbTongTrang.Text = "/ " + tong;

        }
        public List<ChiTietKhachHangThue> getPage(int page)
        {
            var query = from c in ChiTietKhachHangThue.Objects.GetAll().ToList()
                           .Skip(item * page - item).Take(item)
                        select c;
            return query.ToList();

        }
        private ChiTietKhachHangThue GetData(int i)
        {
            ChiTietKhachHangThue kh = new ChiTietKhachHangThue();
            kh.TienCoc = Convert.ToDouble(dgvChiTietKHThue.Rows[i].Cells[1].Value.ToString());
            kh.GhiChu = dgvChiTietKHThue.Rows[i].Cells[2].Value.ToString();
            kh.SoLuong = Convert.ToInt32(dgvChiTietKHThue.Rows[i].Cells[4].Value.ToString());
            kh.TongTien = Convert.ToDouble(dgvChiTietKHThue.Rows[i].Cells[5].Value.ToString());
            return kh;
        }


        private void btnTroVe_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }



        private void dgvChiTietKHThue_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            bool _selectAll = false;
            int n = dgvChiTietKHThue.Rows.Count;
            if (e.ColumnIndex == 0 && n > 0)
            {
                _selectAll = !_selectAll;
                for (int idx = 0; idx < n; idx++)
                {
                    dgvChiTietKHThue.Rows[idx].Selected = _selectAll;
                    dgvChiTietKHThue.Rows[idx].Cells[0].Value = _selectAll;
                }
                dgvChiTietKHThue.Rows[n - 1].Selected = false;
                dgvChiTietKHThue.Rows[n - 1].Cells[0].Value = false;

            }
        }

        private void ChiTietKHTHueForm_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void LoadData()
        {
            gbPhanTrang.Visible = true;
            LoadKH(getPage(1));
            txtTrang.Text = Convert.ToString(1);
            page = 1;
        }
        private ChiTietKhachHangThue getCTKHThue(int i)
        {
            ChiTietKhachHangThue ct = new ChiTietKhachHangThue();
            ct.MaKhachHang = Convert.ToInt32(dgvChiTietKHThue.Rows[i].Cells[1].Value.ToString());
            ct.TienCoc = Convert.ToDouble(dgvChiTietKHThue.Rows[i].Cells[2].Value.ToString());
            ct.GhiChu = dgvChiTietKHThue.Rows[i].Cells[3].Value.ToString();
            ct.MaBangDia = Convert.ToInt32(dgvChiTietKHThue.Rows[i].Cells[4].Value.ToString());
            ct.SoLuong = Convert.ToInt32(dgvChiTietKHThue.Rows[i].Cells[5].Value.ToString());
            ct.TongTien = Convert.ToDouble(dgvChiTietKHThue.Rows[i].Cells[6].Value.ToString());
            return ct;
        }
        private List<ChiTietKhachHangThue> LayChiTietKhThue()
        {
            List<ChiTietKhachHangThue> list = new List<ChiTietKhachHangThue>();
            if (dgvChiTietKHThue.Rows.Count > 0)
            {
                for (int i = 0; i < dgvChiTietKHThue.Rows.Count; i++)
                {
                    try
                    {
                        if (dgvChiTietKHThue.Rows[i].Cells[0].Value != null)
                        {
                            ChiTietKhachHangThue c = getCTKHThue(i);
                            list.Add(c);
                        }
                    }
                    catch (Exception)
                    {

                    }
                }
            }
            return list;
        }

        private void btnSua_Click_1(object sender, EventArgs e)
        {
            try
            {
                bool kq = false;
                List<ChiTietKhachHangThue> list = LayChiTietKhThue();
                if (list.Count > 0)
                {
                    DialogResult que = MessageBox.Show("Bạn chắc chắn muốn Cập Nhật lại thông tin của chi tiết khách hàng đã chọn?", "Hỏi",
                    MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                    if (que == DialogResult.OK)
                    {
                        foreach (ChiTietKhachHangThue item in list)
                        {
                            kq = ChiTietKhachHangThue.Objects.Update(item);
                        }
                        if (kq)
                        {
                            MessageBox.Show("Cập nhật chi tiết khách hàng thành công !");
                            LoadData();
                        }
                    }
                    else
                    {
                        for (int i = 0; i < dgvChiTietKHThue.Rows.Count; i++)
                        {
                            dgvChiTietKHThue.Rows[i].Cells[0].Value = false;
                        }
                    }
                }
                else
                    MessageBox.Show("Vui lòng chọn chi tiết muốn Cập Nhật ", "Thông Tin!", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (System.Exception)
            {
                MessageBox.Show("Lỗi Cập Nhật ", "Lỗi!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }          
          
        }

        private void btnTroVe_Click_1(object sender, EventArgs e)
        {
            this.Dispose();
        }
        private void btnTDau_Click(object sender, EventArgs e)
        {
            if (page > 1)
            {
                LoadKH(getPage(1));
                txtTrang.Text = Convert.ToString(1);
                page = 1;
            }
        }

        private void btnLui_Click(object sender, EventArgs e)
        {
            if (page >= 2)
            {
                page--;
                txtTrang.Text = Convert.ToString(page);
                LoadKH(getPage(page));
            }
        }

        private void btnTien_Click(object sender, EventArgs e)
        {
            if (page < tong)
                page++;
            if (page <= tong)
            {
                txtTrang.Text = Convert.ToString(page);
                LoadKH(getPage(page));
            }
        }

        private void btnTCuoi_Click(object sender, EventArgs e)
        {

            if (page < tong)
            {
                LoadKH(getPage(tong));
                txtTrang.Text = Convert.ToString(tong);
                page = tong;
            }
        }

        private void txtTrang_KeyDown(object sender, KeyEventArgs e)
        {
            int t;
            if (e.KeyData == Keys.Enter)
            {
                try
                {
                    t = Convert.ToInt32(txtTrang.Text);
                    if (t <= tong && t > 0)
                    {
                        LoadKH(getPage(t));
                        txtTrang.Text = Convert.ToString(t);
                        page = t;
                    }
                }
                catch (Exception)
                {
                    MessageBox.Show("Nhâp Trang Muốn Đến");
                }

            }
        }

        private void dgvChiTietKHThue_ColumnHeaderMouseClick_1(object sender, DataGridViewCellMouseEventArgs e)
        {
            bool _selectAll = false;
            int n = dgvChiTietKHThue.Rows.Count;
            if (e.ColumnIndex == 0 && n > 0)
            {
                _selectAll = !_selectAll;
                for (int idx = 0; idx < n; idx++)
                {
                    dgvChiTietKHThue.Rows[idx].Selected = _selectAll;
                    dgvChiTietKHThue.Rows[idx].Cells[0].Value = _selectAll;
                }
                dgvChiTietKHThue.Rows[n - 1].Selected = false;
                dgvChiTietKHThue.Rows[n - 1].Cells[0].Value = false;
            }
        }

        private void txtTrang_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar.ToString().IndexOfAny(@"qwertyuioplkjhgfdsazxcvbnm!@#$%^&*()_+=|\{}[]?><.,';:".ToCharArray()) != -1)
            {
                e.Handled = true;
            }

            else
            {
                e.Handled = false;
            }
        }



    }
}
